package webServlet;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.login.test.VerifyCode;

/**
 * Servlet implementation class checkcodeServlet
 */
@WebServlet("/checkcodeServlet")
public class checkcodeServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public checkcodeServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {

			int width = 200;

			int height = 69;

			BufferedImage verifyImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);


			String randomText = VerifyCode.drawRandomText(width, height, verifyImg);

			request.getSession().setAttribute("checkCode", randomText);
			//System.out.println(request.getSession().getAttribute("checkCode"));
			response.setContentType("image/png");

			OutputStream os = response.getOutputStream(); 
			ImageIO.write(verifyImg, "png", os);

			
			os.flush();

			os.close();// �ر���

		} catch (IOException e) {

			e.printStackTrace();

		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
